home *** CD-ROM | disk | FTP | other *** search
/ Software 2000 / Software 2000 Volume 1 (Disc 1 of 2).iso / utilities / u118.dms / in.adf / top / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-06  |  4.2 KB  |  200 lines

  1. /* Copyright (c) 1988 by Sozobon, Limited.  Author: Tony Andrews
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  */
  11. #include "top.h"
  12.  
  13. /*
  14.  * Low-level i/o routines.
  15.  */
  16.  
  17. /*
  18.  * mode tells what kind of stuff we're reading at the moment.
  19.  */
  20. static    int    mode = -1;
  21.  
  22. #define    BSS    0
  23. #define    DATA    1
  24. #define    TEXT    2
  25.  
  26. static    char    *mnames[] = {
  27. #ifdef NORTHC
  28.     "bss",
  29.     "data",
  30.     "code"
  31. #else
  32.     ".bss",
  33.     ".data",
  34.     ".text"
  35. #endif
  36. };
  37.  
  38. /*
  39.  * Tokens from the current line...
  40.  */
  41. char    *t_line;        /* the entire line */
  42. char    *t_lab;            /* label, if any */
  43. char    *t_op;            /* opcode */
  44. char    *t_arg;            /* arguments */
  45.  
  46. #define    ISWHITE(c)    ((c) == '\t' || (c) == ' ' || (c) == '\n')
  47. /* int
  48. ISWHITE(c)
  49.     char c;
  50.    {return((c) == '\t' || (c) == ' ' || (c) == '\n');
  51.     } */
  52.  
  53. #define    LSIZE    2048    /* max. size of an input line */
  54.  
  55. static    void    tokenize();
  56.  
  57. /*
  58.  * readline() - read the next line from the file
  59.  *
  60.  * readline passes data and bss through to the output, only returning
  61.  * when a line of text has been read. Returns FALSE on end of file.
  62.  */
  63.  
  64. #ifdef NORTHC
  65. static    char    buf[LSIZE];
  66. extern  int out_code;
  67. #endif
  68.  
  69. bool
  70. readline()
  71. {
  72.     char    *fgets();
  73. #ifndef NORTHC
  74.     static    char    buf[LSIZE];
  75. #endif
  76.  
  77.     /*
  78.      * Keep looping until we get a line of text
  79.      */
  80.     for (;;) {
  81.         if (fgets(buf, LSIZE, ifp) == NULL)
  82.             return FALSE;
  83.     
  84.         t_line = buf;
  85.     
  86.         /*
  87.          * Find out if the mode is changing.
  88.          */
  89.         tokenize(buf);
  90.  
  91. #ifdef NORTHC
  92.         /* is it a pseudo-op? */
  93.                 if(stricmp(t_op,"SECTION")==0)
  94.                    {/* New section, find out what type */
  95.                     char typeStr[5];
  96.                     char *parser = t_arg;
  97.                     int  i;
  98.  
  99.                     /* The section type is the second argument */
  100.                     while(*parser!=',' && *parser!='\0')
  101.                         parser++;
  102.                     if(parser!='\0')
  103.                        {parser++;
  104.                         i = 0;
  105.                         while(isalpha(*parser) && i<4)
  106.                            {typeStr[i] = *parser;
  107.                             parser++;
  108.                             i++;
  109.                             }
  110.                         typeStr[i] = '\0';
  111.                      if(stricmp(typeStr,mnames[BSS])==0)
  112.                            {mode = BSS;
  113. /*                            out_code = 0;
  114.                 newBSS(t_arg); */
  115.                 continue;
  116.                             }
  117.                   else if(stricmp(typeStr,mnames[DATA])==0)
  118.                            {mode = DATA;
  119. /*                            out_code = 0;
  120.                 newDATA(t_arg); */
  121.                 continue;
  122.                             }
  123.                   else if(stricmp(typeStr,mnames[TEXT])==0)
  124.                            {mode = TEXT;
  125.                             continue;
  126.                             }
  127.                         }
  128.                     }
  129.           else if(stricmp(t_op, "END" ) == 0)
  130.             continue;
  131.           else if(stricmp(t_op, "XDEF" ) == 0)
  132.            {fputs(buf, ofp);
  133.                     continue;
  134.                     }
  135. #else
  136.         if (t_op[0] == '.') {        /* is it a pseudo-op? */
  137.             if (strcmp(t_op, mnames[BSS]) == 0)
  138.                 mode = BSS;
  139.             else if (strcmp(t_op, mnames[DATA]) == 0)
  140.                 mode = DATA;
  141.             else if (strcmp(t_op, mnames[TEXT]) == 0) {
  142.                 mode = TEXT;
  143.                 continue;
  144.             }
  145.         }
  146. #endif
  147.         if (mode == TEXT)
  148.             return TRUE;
  149. /*        fputs(buf, ofp); */
  150.         store(mode);
  151.     }
  152. }
  153.  
  154.  
  155. static void
  156. tokenize(s)
  157. register char    *s;
  158. {
  159.     static    char    label[LSIZE], opcode[LSIZE], args[LSIZE];
  160.     register int    i;
  161.  
  162.     /*
  163.      * Grab the label, if any
  164.      *
  165.          */
  166.     i = 0;
  167.     while (*s && !ISWHITE(*s) && *s != ':')
  168.         label[i++] = *s++;
  169.     label[i] = '\0';
  170.  
  171.     if (*s == ':')
  172.         s++;
  173.  
  174.     while (ISWHITE(*s))
  175.         s++;
  176.  
  177.     /*
  178.      * Grab the opcode
  179.      */
  180.     i = 0;
  181.     while (*s && !ISWHITE(*s))
  182.         opcode[i++] = *s++;
  183.     opcode[i] = '\0';
  184.  
  185.     while (ISWHITE(*s))
  186.         s++;
  187.  
  188.     /*
  189.      * Grab the arguments
  190.      */
  191.     i = 0;
  192.     while (*s && !ISWHITE(*s))
  193.         args[i++] = *s++;
  194.     args[i] = '\0';
  195.  
  196.     t_lab = label;
  197.     t_op = opcode;
  198.     t_arg = args;
  199. }
  200.